// daemon.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#define MAXFILE 65535
volatile sig_atomic_t _running = 1;
void sigterm_handler(int arg);

int main()
{
	pid_t pc;
	int i,fd,len;
	char *buf="this is a Dameon \n";
	len = strlen(buf);
	pc = fork();
	if (pc < 0){
		printf("Error fork \n");
		exit(1);
	}
	else if (pc > 0)
		exit(0);
	setsid();
	chdir("/");
	umask(0);
	for( i = 0; i< MAXFILE;i++)
		close(i);
	signal(SIGTERM,sigterm_handler);
	if (( fd = open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
		perror("open");
		exit(1);
	}
	while (_running){
		write(fd,buf,len);
		sleep(1);
	}
	close(fd);
}

void sigterm_handler(int arg)
{
	_running = 0; 
}
